//
//  WNBusinessRequest.m
//  Whatnet_iOS
//
//  Created by 北辰 on 2019/5/23.
//  Copyright © 2019 什么网络. All rights reserved.
//

#import "WNBusinessRequest.h"

@implementation WNBusinessRequest

#pragma mark - 发送验证码
+ (void)sendCodeWithPhone:(NSString *)phone Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"/userinfo/sendmessage"];
    NSDictionary *params = @{@"phone":phone};
    [WNNetwork GET:url para:params success:succ failure:fail];
}

#pragma mark - 注册
//手机号、密码登录
+ (void)registerWithPhone:(NSString *)phone pass:(NSString *)pass suserid:(NSString *)suserid Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"userinfo/register"];
    NSMutableDictionary *params = [@{@"phone":phone,@"password":[pass MD5]} mutableCopy];
    if (suserid.length > 0) {
        [params setObject:suserid forKey:@"suserid"];
    }
    [WNNetwork GET:url para:params success:succ failure:fail];
}

#pragma mark - 登录
//手机号、密码登录
+ (void)loginWithPhone:(NSString *)phone pass:(NSString *)pass Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"userinfo/login"];
    NSDictionary *params = @{@"phone":phone,@"password":[pass MD5]};
    [WNNetwork GET:url para:params success:succ failure:fail];
}

#pragma mark - 用户信息
//用户ID、手机号、头像、昵称、真实姓名、银行名称、银行账号、身份证号、会员等级、活跃度、剩余糖果
+ (void)getUserInfoWithID:(NSString *)ID Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%@",kWNBaseURLString,@"userinfo/item/",ID];
//    NSDictionary *params = @{@"id":ID};
    [WNNetwork GET:url para:nil success:succ failure:fail];
}


#pragma mark - 用户信息包含头像的
+ (void)getUserHeadImageSucc:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%@",kWNBaseURLString,@"userinfo/itemImage/",[self userID_String]];
    [WNNetwork GET:url para:nil success:succ failure:fail];
}

#pragma mark - 修改登录密码
+ (void)updateLoginpassWithPass:(NSString *)pwd Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"userinfo/update"];
    NSDictionary *params = @{@"password":[pwd MD5],@"userid":@(NSObject.new.loginManager.userInfo.userid)};
    [WNNetwork POST:url para:params success:succ failure:fail];
}

#pragma mark - 修改支付密码
+ (void)updatePaypassWithPass:(NSString *)pwd Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"userinfo/update"];
    NSDictionary *params = @{@"paypassword":[pwd MD5],@"userid":@(NSObject.new.loginManager.userInfo.userid)};
    [WNNetwork POST:url para:params success:succ failure:fail];
}

#pragma mark - 忘记密码
+ (void)modifyLoginpassWithPass:(NSString *)pwd phone:(NSString *)phone Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"userinfo/updatePsdByPhone"];
    NSDictionary *params = @{@"phone":phone,@"password":[pwd MD5]};
    [WNNetwork POST:url para:params success:succ failure:fail];
}

#pragma mark - 修改用户信息
//昵称、真实姓名、银行名称、银行账号、身份证号、交易密码
+ (void)updateUserInfoWithModel:(WNUserInfo *)userinfo Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"userinfo/update"];
    
    
    
    NSMutableDictionary *dict = [NSMutableDictionary new];
    if (userinfo.nickname) {
        dict[@"nickname"] = userinfo.nickname;
    }
    if (userinfo.name) {
        dict[@"name"] = userinfo.name;
    }
    if (userinfo.cardid) {
        dict[@"cardid"] = userinfo.cardid;
    }
    if (userinfo.cardnum) {
        dict[@"cardnum"] = userinfo.cardnum;
    }
    if (userinfo.bankname) {
        dict[@"bankname"] = userinfo.bankname;
    }
    if (userinfo.userid) {
        dict[@"userid"] = @(userinfo.userid);
    }
    if (userinfo.headimage) {
        dict[@"headimage"] = userinfo.headimage;
    }
    if (userinfo.viplevel) {
        dict[@"viplevel"] = userinfo.viplevel;
    }
    if (userinfo.candynumber) {
        dict[@"candynumber"] = userinfo.candynumber;
    }
    if (userinfo.totalcandynumber) {
        dict[@"totalcandynumber"] = userinfo.totalcandynumber;
    }
    
    [WNNetwork POST:url para:dict success:succ failure:fail];
}

#pragma mark - 公告列表
//查询公告表所有的公告，支持分页
+ (void)getCommentInfoWithPageNum:(NSInteger )pageNum Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd/100",kWNBaseURLString,@"commontinfo/list/",pageNum];
    [WNNetwork GET:url para:nil success:succ failure:fail];
}

#pragma mark - 买单列表
//查询公告表所有的公告，支持分页
+ (void)getTaskInfoWithPageNum:(NSInteger )pageNum Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd/20",kWNBaseURLString,@"transactioninfo/list/",pageNum];
    [WNNetwork GET:url para:nil success:succ failure:fail];
}

#pragma mark - 任务列表
+ (void)getTaskListWithPageNum:(NSInteger )pageNum Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd/20",kWNBaseURLString,@"taskinfo/list/",pageNum];
    [WNNetwork GET:url para:nil success:succ failure:fail];
}

#pragma mark - 查询任务信息
+ (void)checkTaskInfoWithID:(NSString *)ID Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%@",kWNBaseURLString,@"taskinfo/item/",ID];
    [WNNetwork GET:url para:nil success:succ failure:fail];
}

#pragma mark - 获取我的任务列表
+ (void)getMyTaskListWithPageNum:(NSInteger )pageNum status:(NSInteger )status Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd/20",kWNBaseURLString,@"mytaskinfo/list/",pageNum];
    NSDictionary *params = @{
                             @"userid":[self userID_String],
                             @"status":@(status)};
    [WNNetwork GET:url para:params success:succ failure:fail];
}

#pragma mark - 插入我的任务数据
+ (void)insertMyTaskListWithIsdefault:(NSInteger )isdefault taskid:(NSInteger )taskid Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"mytaskinfo/insert"];
    NSDictionary *params = @{
                             @"userid":[self userID_String],
                             @"taskid":@(taskid),
                             @"isdefault":@(isdefault)};
    [WNNetwork POST:url para:params success:succ failure:fail];
}

#pragma mark - 商家列表
//查询商家列表，支持分页
+ (void)getCompanyListWithPageNum:(NSInteger )pageNum Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd/20",kWNBaseURLString,@"companyinfo/list/",pageNum];
    [WNNetwork GET:url para:nil success:succ failure:fail];
}

#pragma mark - 商家商品列表
//根据商家ID查询商品列表，支持分页
+ (void)getGoodsListWithCompanyID:(NSString *)companyID pageNum:(NSInteger )pageNum Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd/20",kWNBaseURLString,@"productinfo/list/",pageNum];
    NSDictionary *params = @{@"companyid":companyID};
    [WNNetwork GET:url para:params success:succ failure:fail];
}

#pragma mark - 查找单个商品
+ (void)getSinglGoos:(NSString *)ID Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%@",kWNBaseURLString,@"productinfo/item/",ID];
    [WNNetwork GET:url para:nil success:succ failure:fail];
}

#pragma mark - 上传步数
//步数上传接口
+ (void)uploadStepCount:(NSString *)count Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"runinginfo/upload"];
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a];
    NSString * MD5key = @"123456@qwe";
    NSString * sign = [NSString stringWithFormat:@"%@%@%@%@",
                       [self userID_String],
                       count,
                       timeString,
                       MD5key];
    NSDictionary *params = @{@"userid":@(NSObject.new.loginManager.userInfo.userid),
                             @"number":count,
                             @"sign":[sign MD5],
                             @"time":timeString};
    [WNNetwork GET:url para:params success:succ failure:fail];
}

#pragma mark - 地址管理
#pragma mark 删除接口
+ (void)deleteAddressWithID:(NSString *)ID Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@/%@",kWNBaseURLString,@"receiveaddress/delete",ID];
    [WNNetwork GET:url para:nil success:succ failure:fail];
}

#pragma mark 新增接口
+ (void)addAddressWithID:(NSString *)ID
                  address:(NSString *)address
                  isdefault:(BOOL)isdefault
                  name:(NSString *)name
                  phone:(NSString *)phone
                    Succ:(WNRequestSuccess)succ
                    fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"receiveaddress/insert"];
    NSDictionary *params = @{@"userid":@(NSObject.new.loginManager.userInfo.userid),
                             @"phone":phone,
                             @"name":name,
                             @"address":address,
                             @"isdefault":isdefault ? @"1":@"0"};
    [WNNetwork POST:url para:params success:succ failure:fail];
}

#pragma mark 查询列表接口
+ (void)getAddressListWithPageNum:(NSInteger )pageNum Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd/20",kWNBaseURLString,@"receiveaddress/list/",pageNum];
    NSDictionary *params = @{@"userid":@(NSObject.new.loginManager.userInfo.userid)};
    [WNNetwork GET:url para:params success:succ failure:fail];
}

#pragma mark 订单列表接口
+ (void)getOrderListWithType:(NSInteger)type pageNum:(NSInteger )pageNum Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd/20",kWNBaseURLString,@"orderinfo/list/",pageNum];
    NSDictionary *params = @{@"userid":@(NSObject.new.loginManager.userInfo.userid),
                             @"status":@(type)};
    [WNNetwork GET:url para:params success:succ failure:fail];
}

#pragma mark - 下单
+ (void)addOrderWithGoodsID:(NSInteger)goodsID
          addressid:(NSInteger )addressid
              price:(CGFloat )price
             number:(NSInteger )number
            Succ:(WNRequestSuccess)succ
            fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"orderinfo/insert"];
    NSDictionary *params = @{@"userid":@(NSObject.new.loginManager.userInfo.userid),
                             @"productid":@(goodsID),
                             @"addressid":@(addressid),
                             @"number":@(number),
                             @"price":@(price)};
    [WNNetwork POST:url para:params success:succ failure:fail];
}

#pragma mark - 商品支付
+ (void)payWithOrderID:(NSInteger)orderID payPwd:(NSString *)pwd Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd/%zd/%@",
                     kWNBaseURLString,
                     @"orderinfo/pay/",
                     NSObject.new.loginManager.userInfo.userid,
                     orderID,[pwd MD5]];
    NSDictionary *params = @{@"userid":@(NSObject.new.loginManager.userInfo.userid)};
    [WNNetwork POST:url para:params success:succ failure:fail];
}

#pragma mark 获取默认地址
+ (void)getDefauleAddressListSucc:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@1/20",kWNBaseURLString,@"receiveaddress/list/"];
    NSDictionary *params = @{@"userid":@(NSObject.new.loginManager.userInfo.userid),
                             @"isdefault":@(1)
    };
    [WNNetwork GET:url para:params success:succ failure:fail];
}

#pragma mark 修改接口
+ (void)updateAddressListWithWithID:(NSString *)ID
              address:(NSString *)address
              isdefault:(BOOL)isdefault
              name:(NSString *)name
              phone:(NSString *)phone
                Succ:(WNRequestSuccess)succ
                fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"receiveaddress/update"];
    NSMutableDictionary *dict = [NSMutableDictionary new];
    if (phone) {
       dict[@"phone"] = phone;
    }
    if (address) {
       dict[@"address"] = address;
    }
    if (name) {
       dict[@"name"] = name;
    }
    dict[@"id"] = ID;
    dict[@"isdefault"] = isdefault ? @"1":@"0";
    dict[@"userid"] = @(NSObject.new.loginManager.userInfo.userid);
    [WNNetwork POST:url para:dict success:succ failure:fail];
}

#pragma mark - 任务列表
#pragma mark - 我的任务
#pragma mark - 我的任务历史
#pragma mark - 雨露交易列表（买单/卖单）
#pragma mark - 近10天每日买单平均值
#pragma mark - 近10天每日卖单平均值
#pragma mark - 交易统计（买单/卖单）
#pragma mark - 步数上传
#pragma mark - 公告列表
#pragma mark - 商品详情
#pragma mark - 商品下单
#pragma mark - 我的订单列表（未支付/已支付）
#pragma mark - 订单详情
#pragma mark - 我的雨露交易列表（买入/卖出/交换中）
#pragma mark - 我的收获地址
#pragma mark - 新增收获地址
#pragma mark - 我的团队
#pragma mark - 邀请好友
#pragma mark - 更新接口

#pragma mark - 订单模块
// /transactioninfo/delete/{id}
#pragma mark 删除接口
+ (void)deleteTransactionInfo:(NSString *)ID Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"transactioninfo/delete"];
    NSDictionary *params = @{@"id":ID};
    [WNNetwork GET:url para:params success:succ failure:fail];
}

#pragma mark 发布订单接口
+ (void)publishTransaction:(WATransactionModel *)model Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"transactioninfo/insert"];
    NSDictionary *params = @{
                             @"number":model.number, @"userid":@(NSObject.new.loginManager.userInfo.userid),
                             @"price":model.price,
                             @"transactiontype":@(model.transactiontype)
    };
    [WNNetwork POST:url para:params success:succ failure:fail];
}

#pragma mark 查询单个接口
+ (void)getTransactionInfo:(NSInteger )ID Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd",kWNBaseURLString,@"transactioninfo/item/",ID];
    [WNNetwork GET:url para:nil success:succ failure:fail];
}
// GET
#pragma mark 查询列表接口--交换中心用
//transactiontype交易类型0表示买1表示卖
//status交易状态 0未完成，1完成
+ (void)getTransactionListWithPageNum:(NSInteger )pageNum
                      transactiontype:(NSInteger )transactiontype
                                 status:(NSInteger )status
                                 phone:(NSString *)phone
                                 Succ:(WNRequestSuccess)succ
                                 fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd/20",kWNBaseURLString,@"transactioninfo/list/",pageNum];
    NSMutableDictionary *dict = [NSMutableDictionary new];
    dict[@"transactiontype"] = @(transactiontype);
    dict[@"status"] = @(status);
    if (phone.length > 0) {
        dict[@"phone"] = phone;
    }
    [WNNetwork GET:url para:dict success:succ failure:fail];
}

#pragma mark 查询列表接口--我的交易用
//transactiontype交易类型0表示买1表示卖
//status交易状态 0未完成，1完成
+ (void)getTransactionListMineWithPageNum:(NSInteger )pageNum
                      transactiontype:(NSInteger )transactiontype
                                 status:(NSInteger )status
                                 dealuserid:(NSInteger )dealuserid
                                 userid:(NSInteger )userid
                                 Succ:(WNRequestSuccess)succ
                                 fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd/20",kWNBaseURLString,@"transactioninfo/list/",pageNum];
    NSMutableDictionary *dict = [NSMutableDictionary new];
//    dict[@"status"] = @(status);
    if (transactiontype >= 0) {
        dict[@"transactiontype"] = @(transactiontype);
    }
    if (dealuserid >= 0) {
        dict[@"dealuserid"] = @(dealuserid);
    }
    if (userid >= 0) {
        dict[@"userid"] = @(userid);
    }
    [WNNetwork GET:url para:dict success:succ failure:fail];
}


#pragma mark 查询交易统计（买单/卖单）
+ (void)getTransactionCountData:(NSString *)companyID pageNum:(NSInteger )pageNum Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",kWNBaseURLString,@"transactioninfo/queryCountData"];
    NSDictionary *params = @{@"companyid":companyID,@"pageNum":@(pageNum),@"pageSize":@(20)};
    [WNNetwork GET:url para:params success:succ failure:fail];
}
// GET
// //{type}
#pragma mark 查询近10天每日买单/卖单平均值 /transactioninfo//
+ (void)getTenDayTransactionDataWithType:(NSInteger)type Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd",kWNBaseURLString,@"transactioninfo/queryLastTenDaysData/",type];
    [WNNetwork GET:url para:nil success:succ failure:fail];
}

#pragma mark 修改接口
//type参数   1 买 /卖 2上传截图  3确认交易 4取消
+ (void)updateTransactioninfoWithType:(NSInteger)Type ID:(NSInteger )ID  status:(NSInteger )status image:(NSString *)imageUrl Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd",kWNBaseURLString,@"transactioninfo/update/",Type];
    NSDictionary *params = @{@"id":@(ID),
                             @"dealuserid":[self userID_String]};
    NSMutableDictionary *dict = [params mutableCopy];
    if (imageUrl) {
        [dict setObject:imageUrl forKey:@"imageurl"];
    }
    if(Type == 4){
        [dict removeObjectForKey:@"dealuserid"];
    }
    [WNNetwork POST:url para:dict success:succ failure:fail];
}

#pragma mark 查询交换统计数据
+ (void)getTransactionTotalDataWithType:(NSInteger)type Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@%zd",kWNBaseURLString,@"transactioninfo/queryCountData/",type];
    [WNNetwork GET:url para:nil success:succ failure:fail];
}

#pragma mark 好友列表
+ (void)getFriendListWithType:(NSInteger)type
                      pageNum:(NSInteger )pageNum
                         Succ:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *address;
    //一级好友
    if (type == 0) {
        address = @"fuserteam/list/";
    }
    //二级好友
    else if (type == 1){
        address = @"suserteam/list/";
    }
    //三级好友
    else{
        address = @"tuserteam/list/";
    }
    NSString *url = [NSString stringWithFormat:@"%@%@%zd/20",
                     kWNBaseURLString,
                     address,
                     pageNum];
    
    NSDictionary *params = @{@"userid":[self userID_String]};
    [WNNetwork GET:url para:params success:succ failure:fail];
}

//http://47.110.231.176/activeinfo/selectVersion
+ (void)getNewVisionSucc:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",
    kWNBaseURLString,
    @"activeinfo/selectVersion"];
    [WNNetwork GET:url para:nil success:succ failure:fail];
}

+ (void)getQiuneTokenSucc:(WNRequestSuccess)succ fail:(WNRequestFailure)fail{
    NSString *url = [NSString stringWithFormat:@"%@%@",
    kWNBaseURLString,
    @"userinfo/getUploadToken"];
    [WNNetwork GET:url para:nil success:succ failure:fail];
}
@end
